package hhm.android.account

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProvider
import com.alibaba.android.arouter.facade.annotation.Route
import com.trello.rxlifecycle4.RxLifecycle
import com.trello.rxlifecycle4.android.ActivityEvent
import hhm.android.account.databinding.ActivityLibraryOneBinding
import hhm.android.library.utils.GetStringListener
import hhm.android.library.utils.OnMultiClickListener
import siau.android.base.*
import siau.android.http.model.ChangeIdentityRequest
import siau.android.library.KeyString

@Route(path = SELECT_IDENTITY)
class CreateLibraryOneActivity : SBBaseActivity() {

    lateinit var viewmodel: CreateLibraryOneViewModel
    lateinit var dataBinding: ActivityLibraryOneBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setTitleWhiteAndTextBlank()
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_library_one)
        viewmodel = ViewModelProvider(this).get(CreateLibraryOneViewModel::class.java)
        dataBinding.viewModel = viewmodel
        dataBinding.lifecycleOwner = this

        viewmodel.pageType = intent.getIntExtra(KeyString.type, -1)

        if (viewmodel.pageType != -1) {
            findViewById<TextView>(R.id.activity_library_one_top).text = getString(R.string.choose_identity2)
            findViewById<TextView>(R.id.activity_library_one_tip).text = getString(R.string.you_are_babys)
            findViewById<TextView>(R.id.activity_library_one_btn).text = getString(R.string.finish)
        }

        val startActivityForResult = registerForActivityResult(IntentIntActivityResultContact()) {
            if (it == 1024) {
                setResult(1024)
                finish()
            }
        }

        viewmodel.choose.observe(this, {
            when (it) {
                viewmodel.chooseMom -> {
                    dataBinding.activityLibraryOneMonIv.setImageResource(R.mipmap.mother_icon_select)
                    dataBinding.activityLibraryOneDadIv.setImageResource(R.mipmap.father_icon)
                    dataBinding.activityLibraryOneDiyIv.setImageResource(R.mipmap.other_identity_icon)
                }
                viewmodel.chooseDad -> {
                    dataBinding.activityLibraryOneMonIv.setImageResource(R.mipmap.mother_icon)
                    dataBinding.activityLibraryOneDadIv.setImageResource(R.mipmap.father_icon_select)
                    dataBinding.activityLibraryOneDiyIv.setImageResource(R.mipmap.other_identity_icon)
                }
                viewmodel.chooseOther -> {
                    dataBinding.activityLibraryOneMonIv.setImageResource(R.mipmap.mother_icon)
                    dataBinding.activityLibraryOneDadIv.setImageResource(R.mipmap.father_icon)
                    dataBinding.activityLibraryOneDiyIv.setImageResource(R.mipmap.other_identity_icon_select)
                }
            }
        })

        dataBinding.activityLibraryOneMonLl.setOnClickListener {
            viewmodel.choose.value = viewmodel.chooseMom
            dataBinding.activityLibraryOneBtn.background = ContextCompat.getDrawable(this, R.drawable.rectangle_circle_maincolor)
        }
        dataBinding.activityLibraryOneDadLl.setOnClickListener {
            viewmodel.choose.value = viewmodel.chooseDad
            dataBinding.activityLibraryOneBtn.background = ContextCompat.getDrawable(this, R.drawable.rectangle_circle_maincolor)
        }

        dataBinding.activityLibraryOneDiyLl.setOnClickListener {
            getStringDialog(this, getString(R.string.input_identity), viewmodel.otherName, object : GetStringListener {
                override fun getString(s: String) {
                    viewmodel.choose.value = viewmodel.chooseOther
                    viewmodel.otherName = s
                    dataBinding.activityLibraryOneDiyTv.text = s
                    dataBinding.activityLibraryOneBtn.background = ContextCompat.getDrawable(
                        this@CreateLibraryOneActivity, R.drawable.rectangle_circle_maincolor
                    )
                }
            })
        }

        dataBinding.activityLibraryOneBtn.setOnClickListener(object : OnMultiClickListener() {
            override fun onMultiClick(v: View?) {
                if (viewmodel.choose.value == viewmodel.chooseMom || viewmodel.choose.value == viewmodel.chooseDad || viewmodel.choose.value == viewmodel.chooseOther) {
                    if (viewmodel.pageType == -1) {
                        val i = Intent(this@CreateLibraryOneActivity, CreateLibraryTwoActivity::class.java)
                        i.putExtra(KeyString.identityType, viewmodel.choose.value)
                        i.putExtra(KeyString.userIdentity, viewmodel.otherName)
                        startActivityForResult.launch(i)
                    } else {
                        showLoadingDialog()
                        val r = ChangeIdentityRequest(SBApplication.userData.familyId, viewmodel.choose.value!!, viewmodel.otherName, 0)
                        HttpHelper().changeIdentity(r)?.compose(RxLifecycle.bindUntilEvent(lifecycle(), ActivityEvent.STOP))?.subscribe({
                            hideLoadingDialog()
                            SBApplication.userData.identityType = viewmodel.choose.value!!
                            SBApplication.userData.userIdentity = viewmodel.otherName
                            setResult(RESULT_OK)
                            finish()
                        }, {
                            hideLoadingDialog()
                            showToast(it.message)
                        })
                    }
                }
            }
        })
    }
}